package cn.com.duiba.millionaire.center.biz.service.impl;

import cn.com.duiba.millionaire.center.api.enums.AppConfigProNameEnum;
import cn.com.duiba.millionaire.center.biz.dao.MillionaireAppConfigDao;
import cn.com.duiba.millionaire.center.biz.entity.MillionaireAppConfigEntity;
import cn.com.duiba.millionaire.center.biz.service.MillionaireAppConfigService;
import com.google.common.collect.Lists;
import org.apache.commons.collections.CollectionUtils;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.data.redis.core.RedisTemplate;
import org.springframework.stereotype.Service;

import javax.annotation.Resource;
import java.util.ArrayList;
import java.util.List;
import java.util.Objects;
import java.util.concurrent.TimeUnit;

/**
 * Created by sunchangji on 2018/2/24.
 */
@Service
public class MillionaireAppConfigServiceImpl implements MillionaireAppConfigService {
    @Autowired
    private MillionaireAppConfigDao millionaireAppConfigDao;
    @Resource(name = "redisTemplate")
    private RedisTemplate<String, List<Long>> redisTemplate;

    @Override
    public int batchInsert(List<Long> appIds, String propName) {
        if (CollectionUtils.isEmpty(appIds)) {
            return -1;
        }

        List<Long> filterAppIds = millionaireAppConfigDao.filterAppIds(appIds);
        if(CollectionUtils.isNotEmpty(filterAppIds)){
            appIds.removeAll(filterAppIds);
        }
        if(appIds.isEmpty()){
            return -1;
        }
        AppConfigProNameEnum proNameEnum = AppConfigProNameEnum.getByProName(propName);
        List<MillionaireAppConfigEntity> entities = new ArrayList<>();
        for (Long appId : appIds) {
            MillionaireAppConfigEntity entity = new MillionaireAppConfigEntity();
            entity.setAppId(appId);
            entity.setPropName(propName);
            entity.setPropValue(proNameEnum.getPropValue());
            entities.add(entity);
        }
        int line = millionaireAppConfigDao.batchInsert(entities);
        if (line > 0) {
            //失效redis中的值
            deleteAppConfigRedis();
        }

        return line;
    }

    @Override
    public int deleteByIds(List<Long> appIds) {
        if (CollectionUtils.isEmpty(appIds)) {
            return -1;
        }
        int line = millionaireAppConfigDao.deleteByIds(appIds);
        if (line > 0) {
            //失效redis中的值
            deleteAppConfigRedis();
        }
        return line;
    }

    @Override
    public List<Long> findAppIdByPropName(String propName) {
        Objects.requireNonNull(propName);
        String key = AppConfigProNameEnum.PRIX_REDIS + propName;
        List<Long> appIds = redisTemplate.opsForValue().get(key);
        if (CollectionUtils.isNotEmpty(appIds)) {
            return appIds;
        }
        appIds = millionaireAppConfigDao.findAppIdByPropName(propName);
        if (CollectionUtils.isNotEmpty(appIds)) {
            redisTemplate.opsForValue().set(key, appIds, 12, TimeUnit.HOURS);
        }
        return appIds;
    }

    /**
     * 删除redis中app配置
     */
    private void deleteAppConfigRedis() {
        String aliPayDuibaKey = AppConfigProNameEnum.PRIX_REDIS + AppConfigProNameEnum.ALIPAY_DUIBA.getPropName();
        String offLinePay = AppConfigProNameEnum.PRIX_REDIS + AppConfigProNameEnum.NO_ALIPAY.getPropName();
        List<String> keys = Lists.newArrayList(aliPayDuibaKey, offLinePay);
        redisTemplate.delete(keys);
    }

}
